package com.jlboot.model.po.sys;

import com.jfinal.plugin.activerecord.Db;
import com.jlboot.core.cache.CacheNameConstants;
import com.jlboot.core.consts.Consts;
import com.jlboot.core.util.CacheUtil;
import com.jlboot.core.util.DateUtil;
import com.jlboot.core.base.Models;
import com.jfinal.plugin.activerecord.Model;
import com.jlboot.core.plugins.ip.IpKit;
import com.jlboot.core.util.LoginUtil;

import java.util.Date;

/**
 * Created by 51594 on 2022/7/27.
 */
public class SysOnline extends Model<SysOnline> {
    public static final SysOnline dao = new SysOnline().dao();
    public static final String table="sys_online";
    public Models mod = Models.n(this);

    public static String getNextId(){
        return SysIdGenerator.dao.getNextId(table);
    }

    public SysOnline setCommon(){
        Date now=new Date();
        this.set("id",SysIdGenerator.dao.getNextId(table));
        this.set("del_tag", Consts.COMMON_NONE_DEL);
        this.set("create_time",now);
        this.set("update_time",now);
        this.set("create_by", LoginUtil.getUserId());
        this.set("update_by",LoginUtil.getUserId());
        return this;
    }

    /**
     * @param userId
     * @param ip
     */
    public void saveLogin(String sessId, String userId, String ip, String os, String browser) {
        SysOnline online = SysOnline.dao.findById(sessId);
        String login_addr = IpKit.n().getAddress(ip);
        long times = DateUtil.getYYYYMMHHMMSS();
        if (online == null) {
            SysUser user = SysUser.dao.findById(userId);
            if (user != null) {

                online = new SysOnline()
                        .setCommon()
                        .set("user_id", userId)
                        .set("sess_id", sessId)
                        .set("login_os", os)
                        .set("login_browser", browser)
                        .set("login_addr", login_addr)
                        .set("login_id", user.mod.getStr("login_id"))
                        .set("user_name", user.mod.getStr("user_name"));

                online.set("last_login", new Date()).set("login_ip", ip)
                        .set("last_active", new Date()).set("last_active_int", times).save();
            }
        } else {
            online.set("last_login", new Date()).set("login_ip", ip)
                    .set("last_active", new Date()).set("last_active_int", times).update();
        }

        CacheUtil.put(CacheNameConstants.system_last_active_time,sessId,System.currentTimeMillis());

    }

    /**
     * 记录最后活动时间
     */
    public void saveActive(String sessId) {
        Long b=CacheUtil.get(CacheNameConstants.system_last_active_time,sessId);
        long e = System.currentTimeMillis();
        if(b==null){
            CacheUtil.put(CacheNameConstants.system_last_active_time,sessId,System.currentTimeMillis());
            return;
        }
        //1分钟记录一次
        if ((e - b) > 1000*60) {
            SysOnline online = SysOnline.dao.findFirst("select * from sys_online where sess_id=?",sessId);
            long times = DateUtil.getYYYYMMHHMMSS();
            if (online != null) {
                online.set("last_active", new Date()).set("last_active_int", times).update();
            }
            CacheUtil.put(CacheNameConstants.system_last_active_time,sessId,e);
        }
    }

    /**
     * 统计在线人数
     *
     * @return
     */
    public String getOnlineUserInfo() {
        Date date_15 = DateUtil.getAddDate(new Date(), -15 * 60 * 1000L);
        Date date_60 = DateUtil.getAddDate(new Date(), -60 * 60 * 1000L);
        Date date_360 = DateUtil.getAddDate(new Date(), -360 * 60 * 1000L);
        Date date_1440 = DateUtil.getAddDate(new Date(), -1440 * 60 * 1000L);
        int user_15 = Db.queryInt("select count(*) from sys_online where last_active >?", date_15);
        int user_60 = Db.queryInt("select count(*) from sys_online where last_active >?", date_60);
        int user_360 = Db.queryInt("select count(*) from sys_online where last_active >?", date_360);
        int user_1440 = Db.queryInt("select count(*) from sys_online where last_active >?", date_1440);
        return "15分钟在线<span>" + user_15 + "</span>人，1小时在线<span>" + user_60
                + "</span>人，6小时在线<span>" + user_360 + "</span>人，24小时在线<span>" + user_1440 + "</span>人";
    }

}
